package com.hkts.reportform.sodata.serviceImp;

import com.hkts.reportform.sodata.dao.SoDao;
import com.hkts.reportform.sodata.service.SoService;
import com.hkts.reportform.utils.CustomPage;
import com.hkts.reportform.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("soServiceImp")
public class SoServiceImp implements SoService {
    @Autowired
    private SoDao soDao;
    @Override
    public CustomPage salesOrderList(String dateType, String beginDate, String endDate,CustomPage page) {
        List<String> dateArray = DateUtil.getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> countOrderExecution = soDao.getCountOrderExecution(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        int count = countOrderExecution.size();
        page.setTotalRows(count);
        int firstRow = page.getPageSize() * (page.getPageNumber() - 1) + 1;
        int lastRow = page.getPageSize() * page.getPageNumber();
        List<Map<String, Object>> list = soDao.getSalesOrderList(dateArray.get(0), dateArray.get(dateArray.size() - 1),
                firstRow + "",
                lastRow + "");

        Float zero = (float) 0;
        float ifhquantity;
        NumberFormat numberFormat = NumberFormat.getInstance();
        List<Map<String, Object>> soList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            float iquantity = Float.parseFloat(list.get(i).get("iQuantity").toString());
            ifhquantity = Float.parseFloat(list.get(i).get("iFHQuantity").toString());
            float notys = iquantity - ifhquantity;
            String rate = numberFormat.format(ifhquantity / iquantity * 100);
            map.put("iQuantity",iquantity);
            map.put("notys",notys);
            map.put("rate",rate);
            map.put("cSOCode",list.get(i).get("cSOCode").toString());
            map.put("dverifysystime",list.get(i).get("dverifysystime").toString());
            List<Map<String, Object>> invNameBySoid = soDao.getCInvNameBySoid(list.get(i).get("ID").toString());
            StringBuffer buffer = new StringBuffer();
            for (int j = 0; j < invNameBySoid.size(); j++) {
                if (j == 0) {
                    buffer.append(invNameBySoid.get(j).get("cInvName"));
                } else {
                    if (!buffer.toString().contains(invNameBySoid.get(j).get("cInvName").toString())) {
                        buffer.append("," + invNameBySoid.get(j).get("cInvName"));
                    }
                }
            }
            map.put("cInvNames", buffer.toString());
            soList.add(map);
        }
        page.setResultData(soList);
        return page;
    }

    @Override
    public Map<String,  Object> salesOrderStatistics(String dateType, String beginDate, String endDate) throws ParseException {
        List<String> dateArray = DateUtil.getDateArray(dateType, beginDate, endDate);
        Map<String, Object> mapResult = new HashMap<>();
        List<Map<String, Object>> soList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list = soDao.getSalesOrderStatistics(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        Map<String, Map<String, Object>> map = new HashMap<>();
        for (int g = 0; g < list.size(); g++) {
            if (!map.containsKey(list.get(g).get("cInvCode"))) {
                Map<String, Object> valueMap = new HashMap<String, Object>();
                List<String> dates = new ArrayList<>();
                List<String> valuess = new ArrayList<>();
                dates.add(list.get(g).get("ddate").toString());
                valuess.add(list.get(g).get("iFHQuantity").toString());
                valueMap.put("ddate", dates);
                valueMap.put("cInvName", list.get(g).get("cInvName"));
                valueMap.put("iFHQuantity", valuess);
                map.put(list.get(g).get("cInvCode").toString(), valueMap);
            } else {
                Map<String, Object> valueMap = map.get(list.get(g).get("cInvCode"));
                List<String> dDate = (List<String>) valueMap.get("ddate");
                List<String> iquantity = (List<String>) valueMap.get("iFHQuantity");
                dDate.add(list.get(g).get("ddate").toString());
                iquantity.add(list.get(g).get("iFHQuantity").toString());
                valueMap.put("iFHQuantity", iquantity);
                valueMap.put("ddate", dDate);
                map.put(list.get(g).get("cInvCode").toString(), valueMap);
            }
        }
        int temp = 0;
        Float zero = (float) 0;
        for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {

            Map<String, Object> result = new HashMap<>();
            String mapKey = entry.getKey();
            Map<String, Object> mapValue = entry.getValue();
            List<String> dDate = (List<String>) mapValue.get("ddate");
            List<String> ifhquantity = (List<String>) mapValue.get("iFHQuantity");

            result.put("name", mapValue.get("cInvName"));
            List<Float> valuess = new ArrayList<>();

            for (int i = 0; i < dateArray.size(); i++) {
                if (dDate.contains(dateArray.get(i))) {
                    valuess.add(Float.parseFloat(ifhquantity.get(dDate.indexOf(dateArray.get(i))).toString()));
                } else {
                    valuess.add(zero);
                }
            }
            temp = 0;
            for (int i = 0; i < valuess.size(); i++) {
                if (valuess.get(i) > 0) {
                    result.put("data", valuess);
                    soList.add(result);
                    break;
                }
            }
        }
        if(soList.size() == 0){
            mapResult.put("categories", 0);
            mapResult.put("series",0);
        }else {
            mapResult.put("categories", DateUtil.dateListConvert(dateArray));
            mapResult.put("series",soList);
        }

        return mapResult;
    }

    @Override
    public List<Map<String, Object>> sOdStttc(String dateType, String beginDate, String endDate) {
        List<String> dateArray = DateUtil.getDateArray(dateType, beginDate, endDate);
        Map<String, Object> mapResult = new HashMap<>();
        List<Map<String, Object>> soList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list = soDao.getSsOdSttts(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        Map<String, Map<String, Object>> map = new HashMap<>();
        List<Map<String,  Object>> valuess = new ArrayList<>();
        for (int g = 0; g < list.size(); g++) {
            Map<String, Object> valueMap = new HashMap<String, Object>();
            List<String> dates = new ArrayList<>();

            valueMap.put("ddate", list.get(g).get("ddate").toString());
            valueMap.put("cInvName", list.get(g).get("cInvName"));
            valueMap.put("iFHQuantity", list.get(g).get("iFHQuantity").toString());
            valuess.add(valueMap);
        }
        return valuess;
    }
    //销售过磅单
    @Override
    public CustomPage salesWeighingList(String dateType, String beginDate, String endDate,CustomPage page) {
        List<String> dateArray = DateUtil.getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> countOrderExecution = soDao.getCountSalesWeighingExecution(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        int count = countOrderExecution.size();
        page.setTotalRows(count);
        int firstRow = page.getPageSize() * (page.getPageNumber() - 1) + 1;
        int lastRow = page.getPageSize() * page.getPageNumber();
        List<Map<String, Object>> list = soDao.getSalesWeighingList(dateArray.get(0), dateArray.get(dateArray.size() - 1),
                firstRow + "",
                lastRow + "");
        NumberFormat numberFormat = NumberFormat.getInstance();
        List<Map<String, Object>> soList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();

            float fFirst = Float.parseFloat(list.get(i).get("fFirst").toString());
            float fSecond = Float.parseFloat(list.get(i).get("fSecond").toString());
            float fSuttle = Float.parseFloat(list.get(i).get("fSuttle").toString());
            map.put("fFirst",fFirst);
            map.put("fSecond",fSecond);
            map.put("fSuttle",fSuttle);
            map.put("cSVCode",list.get(i).get("cSVCode").toString());
            map.put("cVehCode",list.get(i).get("cVehCode").toString());
            map.put("dFirstDate",list.get(i).get("dFirstDate").toString());
            List<Map<String, Object>> invNameByWeighinid = soDao.getCInvNameByWeighingId(list.get(i).get("ID").toString());
            StringBuffer buffer = new StringBuffer();
            for (int j = 0; j < invNameByWeighinid.size(); j++) {
                if (j == 0) {
                    buffer.append(invNameByWeighinid.get(j).get("cInvName"));
                } else {
                    if (!buffer.toString().contains(invNameByWeighinid.get(j).get("cInvName").toString())) {
                        buffer.append("," + invNameByWeighinid.get(j).get("cInvName"));
                    }
                }
            }
            map.put("cInvNames", buffer.toString());

            soList.add(map);
        }
        page.setResultData(soList);
        return page;
    }
    @Override
    public Map<String,Object> getCarNumAndTunnage (){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(DateUtil.getYesterday(0));
        List<Map<String, Object>> salesWeighingByData = soDao.getCarNumAndTunnageByData(format, format);
        Map<String, Object> map = new HashMap<>();
        Float zero = (float) 0;
        for (int i = 0; i < salesWeighingByData.size(); i++){
            if (salesWeighingByData.get(i).get("fSuttle") == null ){
                float fSuttle = zero;
                map.put("fSuttle",fSuttle);
            }else {
                float fSuttle = Float.parseFloat(salesWeighingByData.get(i).get("fSuttle").toString());
                map.put("fSuttle",fSuttle);
            }
            List<Map<String, Object>> countSalesWeighingExecution = soDao.getCountSalesWeighingExecution(format, format);
            for (int j = 0; j < countSalesWeighingExecution.size(); j++){
                map.put("carNumber",Integer.parseInt(countSalesWeighingExecution.get(i).get("count").toString()));
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> getSalesWeighingByData(String dateType, String beginDate, String endDate) throws ParseException {
        List<String> dateArray = DateUtil.getDateArray(dateType, beginDate, endDate);
        List<Map<String, Object>> maps = soDao.getSalesWeighingByData(dateArray.get(0), dateArray.get(dateArray.size() - 1));
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> mapCount = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        List<Integer> listcCount = new ArrayList<Integer>();
        List<Integer> listResult = new ArrayList<Integer>();
        List<Map<String, Object>> lm = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < maps.size(); i++) {
            list.add(maps.get(i).get("data").toString());
            listcCount.add(Integer.parseInt(maps.get(i).get("count").toString()));
        }
        for (int i = 0; i < dateArray.size(); i++) {
            if (list.contains(dateArray.get(i))) {
                listResult.add(listcCount.get(list.indexOf(dateArray.get(i))));
            } else {
                listResult.add(0);
            }
        }
        map.put("categories", DateUtil.dateListConvert(dateArray));
        mapCount.put("data", listResult);
        lm.add(mapCount);
        map.put("series", lm);
        return map;
    }
}
